const lodash = require('lodash');
const service = {

    init(tablename, primaryKey, column, addSQL, editSQL, querySQL, listSQL, requiredSQL) {
        let camelCaseTableName = lodash.camelCase(tablename);
        let camelCaseTableNameFirst = lodash.upperFirst(camelCaseTableName);
        let telController =
            `'use strict';

const Controller = require('egg').Controller;
const lodash = require('lodash');

class ${camelCaseTableNameFirst}Controller extends Controller {
    /**
     * 获取列表
     */
    async querylist() {
        const { ctx } = this;
        const { queryParams,pageSize,page } = ctx.request.body;
        let data = await ctx.service.${camelCaseTableName}.querylist(queryParams,pageSize,page);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {tabledata:data}
        };
    }

    /**
     * 新增
     */
    async add() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let submitBody = ctx.request.body;
        //验证必填项
        await app.utils.validator.required(${getFormColumn(requiredSQL, primaryKey)},submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(${getFormColumn(addSQL, primaryKey)},submitBody);
        ${getPkStep(primaryKey)}
        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.create_by = userid;
        submitData.create_time = new Date();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //保存
        let rs = await ctx.service.${camelCaseTableName}.create(submitData);
        ctx.body = {
            code: 200,
            msg: "保存成功",
            data: {},
        };
    }

    /**
     * 编辑
     */
    async edit() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let submitBody = ctx.request.body;
        //验证主键是否存在
        await app.utils.validator.required(${getFormColumn(requiredSQL, primaryKey, true)},submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(${getFormColumn(editSQL, primaryKey)},submitBody);
        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //保存
        let rs = await ctx.service.${camelCaseTableName}.update(submitData,submitBody.${primaryKey.t_column_name});
        ctx.body = {
            code: 200,
            msg: "编辑成功",
            data: {},
        };
    }

    /**
     * 删除
     */
    async delete() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let submitBody = ctx.request.body;
        //验证主键是否存在
        await app.utils.validator.required(${getFormColumn(requiredSQL, primaryKey, true)},submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(${getFormColumn(editSQL, primaryKey, true)},submitBody);

        //操作相关信息
        //let userid = await ctx.service.auth.getColonyUserId();
        //submitData.update_by = userid;
        //submitData.update_time = new Date();
        //删除
        let rs = await ctx.service.${camelCaseTableName}.delete(submitData.${primaryKey.column_name});
        ctx.body = {
            code: 200,
            msg: "删除成功",
            data: {},
        };
    }

    /**
     * 通过主键查询
     */
    async querybyid() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let {${primaryKey.t_column_name}} = ctx.request.body;
        
        //查询
        let rs = await ctx.service.${camelCaseTableName}.queryById(${primaryKey.t_column_name});
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }

}

module.exports = ${camelCaseTableNameFirst}Controller;`;
        return telController;
    }

}

module.exports = service;

/**
 * 获取数组
 * @param {*} columnData 
 */
function getFormColumn(columnData, primaryKey, sign) {
    let retData = [];
    let pk = "";
    let pkRs = Object.prototype.toString.call(primaryKey) === '[object Object]';
    if (pkRs) {
        //排除主键
        pk = primaryKey.column_name;
        if (sign == true) {
            return `['${pk}']`;
        }
    }
    for (var i = 0; i < columnData.length; i++) {
        if (columnData[i].column_name != pk) {
            retData.push(`'${columnData[i].column_name}'`);
        }
    }

    return `[${retData.join(",")}]`;
}

/**
 * 得到主键的生产过程
 */
function getPkStep(primaryKey) {
    if (primaryKey.t_column_extra == "auto_increment") {
        //表示自增组件
        return ``;
    }
    //返回一个uuid
    return `
    //添加组件值
    let ${primaryKey.column_name}_uuid = await app.utils.uuid.v1();
    submitData.${primaryKey.column_name} = ${primaryKey.column_name}_uuid;
    `;
}